<%@page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!-- public quote -->
<%@include file="/jsp/comm_css_include.jsp"%>
<!-- jqgrid-->
<link
	href="${basePath}/css/plugins/jqgriddemo/css/ui.jqgrid-bootstrap.min.css"
	rel="stylesheet" />
<!doctype html>
<html>
<head>
<style type="text/css">
* {
	margin: 0px;
	padding: 0px;
	font-size: 12px;
}

.laydate-icon {
	height: 20px;
}
</style>
</head>
<body class="gray-bg">
	<div class="wrapper wrapper-content animated fadeInRight">
		<div class="iboxtitle">
			<span><i class="glyphicon glyphicon-map-marker"></i>&nbsp;基础信息管理>公告管理</span>
		</div>

		<div class="ibox-content">
			<div role="form" class="form-inline">
				<div class="form-group">
					<label class="control-label">公告标题：</label> <input type="text"
						placeholder="公告标题" id="titleNotice" name="titleNotice"
						class="form-control" value="">
				</div>
				<div class="form-group">
					<label class="control-label">公告有效期：</label> <input
						id="dateValidStart" name="dateValidStart" placeholder="开始时间"
						class="laydate-icon form-control"> - <input
						id="dateValidEnd" name="dateValidEnd" placeholder="结束时间"
						class="laydate-icon form-control">
				</div>
				<button class="zhixcx" type="button" onclick="query();">执行查询</button>
			</div>
		</div>
		<div class="ibox-content">
			<button type="button" class="btn dpbtn_green" onclick="add();">新增</button>
			<button type="button" class="btn dpbtn_blue" onclick="disable();">禁用</button>
			<button type="button" class="btn dpbtn_red" onclick="enable();">启用</button>
			<button type="button" class="btn dpbtn_red" onclick="del();">删除</button>
		</div>
		<div class="ibox-content">
			<div class="jqGrid_wrapper">
				<table id="table_list_1"></table>
				<div id="pager_list_1"></div>
			</div>
		</div>
	</div>


	<!-- public quote -->
	<%@include file="/jsp/comm_js_include.jsp"%>

	<!-- jqGrid -->
	<script type="text/javascript" charset="utf-8"
		src="${basePath}/js/plugins/jqgriddemo/js/jquery.jqGrid.min.js"></script>
	<script type="text/javascript" charset="utf-8"
		src="${basePath}/js/plugins/jqgriddemo/js/grid.locale-cn.min.js"></script>
	<!-- layerDate plugin javascript -->
	<script src="${basePath}/js/plugins/layer/laydate/laydate.js"></script>

	<!--
    	作者：zhangpp
    	时间：2017-04-06
    	描述：js
    -->
	<script type="text/javascript">
	    var firstQuery = true;
	    var winWidth = $(window).width();
		var winHeight = $(window).height();
	    jQuery(function($) { 
	    	var start = {
    			  elem: '#dateValidStart', 
  		          event: 'focus' ,
    			  format: 'YYYY-MM-DD',
    			  max: '2099-06-16', //最大日期
    			  istime: true,
    			  istoday: false,
    			  choose: function(datas){
    			     end.min = datas; //开始日选好后，重置结束日的最小日期
    			     end.start = datas; //将结束日的初始值设定为开始日
    			  }
    			};
	    	
	    	var end = {
	    			  elem: '#dateValidEnd', 
	  		          event: 'focus' ,
	    			  format: 'YYYY-MM-DD',
	    			  max: '2099-06-16',
	    			  istime: true,
	    			  istoday: false,
	    			  choose: function(datas){
	    			    start.max = datas; //结束日选好后，重置开始日的最大日期
	    			  }
	    			};
	    	
	    	laydate(start);
	    	laydate(end);
	    	
	    	getData();
	    });
	    
	    var getData = function()
	    {
	  var winWidth = $('div.ibox-content').width();
	        winHeight = document.body.offsetHeight - 400;
	     	var win = winWidth / 4 - 20;
  	 	   $.jgrid.defaults.styleUI = 'Bootstrap';
  	 	   
		  jQuery("#table_list_1").jqGrid
		  ({
			url:'${basePath}/OtherNoticeController/list',
	        datatype : "json",
	        colNames : [ '主键', '公告有效期', '公告标题','状态'],
	        colModel : [ 
	                     {name: 'replaceId', index: 'id', hidden:true},
	                     {name: 'dateValid',index: 'dateValid',width: win,align: "center",formatter:function(a,b,c){
	                    	 return dateDiff(a) ? formateStringDate(a) : ('<span style="color:#FC1515;">'+  formateStringDate(a) + '</span>');
	                     }},
	                     {name: 'titleNotice',index: 'titleNotice',width: win,align: "center"},
	                  	 {name: 'dpStatusDisplay',index: 'dpStatus',width: win,align: "center",
	                    	 formatter: function (cellvalue, options, rowObject) 
	            			 {	
	            				 if(rowObject.dpStatus == "1")
	            				 {
	            					 return '<span style="color: green;">正常</span>';
	            				 }
	            				 
	            				 if(rowObject.dpStatus == "2")
	            				 {
	            					 return '<span style="color:#FC1515;">禁用</span>';
	            				 }
	            			 }	  
	                  	 }
	                   ],
              mtype : "post",
              height: winHeight,
              width:winWidth,  
              autowidth: true,
              shrinkToFit: true,
              rowNum: 15,
	           rowList: [15, 30, 45],
              viewrecords: true,
              pager: "#pager_list_1",
              hidegrid: false,
              multiselect : true,
              altRows:true,
              altclass:'someClass',
              ondblClickRow:function(rowId,v,e)
              {
            	  var rowData = $("#table_list_1").jqGrid("getRowData", rowId);
            	  var id = rowData.replaceId;
            	  addlayer=layer.open({
    		    	  type: 2,
    		    	  title: '修改公告',
    		    	  shadeClose: true,
    		    	  shade: 0.8,
    		    	  area: ['50%', '90%'],
    		    	  content: '${basePath}/web/system/base/otherNoticeDetail.jsp?id=' + id,
    		    	  end: function () 
    		    	  {
    		    		  refresh();
    		          }
    	    	});
              },
              jsonReader : {
            	  root:"result.rows",
            	  page: "result.page",
            	  total: "result.total",
            	  records: "result.records",
            	  repeatitems: false,
            	  id: "replaceId"
              },
              loadComplete:function(datas){
            	  if (datas.flag == "100100") {
                      layer.msg("查询成功!", {icon: 6, time: 1000});
                      return;
                  }
            	  if(datas.flag == '100802') {
    				  top.location.href = thisLoginUrl;
    				  return;
    			  }

            	  layer.msg(datas.message, {icon: 2});
              }
	      });
	    }	    
	    
	    window.onresize = function()
	    {
	    	$("#table_list_1").setGridWidth($(window).width()-80);
	    }
	    
	    var query = function()
	    {
	    	var postData = $("#table_list_1").jqGrid("getGridParam", "postData");
	    	var data = {};
	    	data.titleNotice = document.getElementById('titleNotice').value || '';
	    	data.dateValidStart = document.getElementById('dateValidStart').value.trim() || '';
    		data.dateValidEnd = document.getElementById('dateValidEnd').value.trim() || '';
	    	$.extend(postData, data);
	    	refresh();
	    }
	    
	    var refresh = function()
	    {
	    	$("#table_list_1").jqGrid("setGridParam", { search: true }).trigger("reloadGrid", [{ page: 1}]);
	    }
	    
	    var addlayer;
	    var add = function()
	    {
	       
	    	addlayer=layer.open({
		    	  type: 2,
		    	  title: '新增公告',
		    	  shadeClose: true,
		    	  shade: 0.8,
		    	  area: ['50%', '70%'],
		    	  content: '${basePath}/web/system/base/otherNoticeDetail.jsp',
		    	  end: function () 
		    	  {
		    		  refresh();
		          }
	    	});
	    }
	    
	    function formateStringDate(sDate1){
	    	if(!sDate1 || sDate1 == ''){
	    		return '-';
	    	}
	    	var aDate  =  sDate1.split(" ");
	    	return aDate[0];
	    }
	    var del = function()
	    {
	    	
	    	var uid = $('#table_list_1').jqGrid('getGridParam','selarrrow').join(",");
	    	if(!uid) {
	    		layer.msg("请选择要删除的公告！", {icon: 5});
	    		return;
	    	}
	    	layer.confirm('您确认要删除吗？', {
  			  btn: ['确定','取消'] //按钮
  			},function(){
  				layer.closeAll();
  				$.dpAjax({
  	 	    		url:'${basePath}/OtherNoticeController/remove',
  	     			data:{
  	     				"id":uid
  	     			},
  	     			success: function(datas){
  	     			  if(datas.flag == "100100")
  	 					  {
  	     				 	  layer.msg("删除执行成功！", {icon: 6});
  	     				 	  refresh();
  	 						  return;
  	 					  }
  	 					  if(datas.flag == "100200")
  	 	                  {
  	 						  $.fn.showDatas({"datas":datas});
  	 	                	  return;
  	 	                  }
  	 					  layer.msg(datas.message, {icon: 2});
  	                 }
  	 	    	});
  			});
	    }
	    
	    var disable = function()
	    {
	    	var uid = $('#table_list_1').jqGrid('getGridParam','selarrrow').join(",");
	    	if(!uid) {
	    		layer.msg("请选择要禁用的公告！", {icon: 5});
	    		return;
	    	}
	    	layer.confirm('您确认要禁用吗？', {
	  			  btn: ['确定','取消'] //按钮
  			},function(){
  				layer.closeAll();
  				$.dpAjax({
  	 	    		url:'${basePath}/OtherNoticeController/disable',
  	     			data:{
  	     				"id":uid
  	     			},
  	     			success: function(datas){
  	     			  if(datas.flag == "100100")
  	 					  {	  
  	     				 	  layer.msg("禁用执行成功！", {icon: 6});
  	     				 	  refresh();
  	 						  return;
  	 					  }
  	 					  if(datas.flag == "100200")
  	 	                  {
  	 						  $.fn.showDatas({"datas":datas});
  	 	                	  return;
  	 	                  }
  	 					  layer.msg(datas.message, {icon: 2});
  	                 }
  	 	    	});
  				
  			});
	    }
	    
	    var enable = function()
	    {
	    	var uid = $('#table_list_1').jqGrid('getGridParam','selarrrow').join(",");
	    	if(!uid) {
	    		layer.msg("请选择要启用的公告！", {icon: 5});
	    		return;
	    	}
	    	layer.confirm('您确认要启用吗？', {
	  			  btn: ['确定','取消'] //按钮
			},function(){
				layer.closeAll();
				$.dpAjax({
	 	    		url:'${basePath}/OtherNoticeController/enable',
	     			data:{
	     				"id":uid
	     			},
	     			success: function(datas){
	     			  if(datas.flag == "100100")
	 					  {	  
	     				 	  layer.msg("启用执行成功！", {icon: 6});
	     				 	  refresh();
	 						  return;
	 					  }
	 					  if(datas.flag == "100200")
	 	                  {
	 						  $.fn.showDatas({"datas":datas});
	 	                	  return;
	 	                  }
	 					  layer.msg(datas.message, {icon: 2});
	                 }
	 	    	});
			});
	    }
	    
	  //计算是否到期 
	    function dateDiff(sDate1){
		    if(sDate1 && sDate1 != ''){
		        var  aDate,  oDate1,  iDays  
		        aDate  =  sDate1.split("-")  
		        oDate1  =  new  Date(aDate[1]  +  '-'  +  aDate[2].split(" ")[0]  +  '-'  +  aDate[0]) //转换为12-18-2006格式  
		        
		        sDate2  =  new  Date(); 
		        var year2 = sDate2.getFullYear();
		        var month2 = sDate2.getMonth() + 1;
		        var day2 = sDate2.getDate();
		        var oDate2 = new  Date(month2 + '-' + day2 + '-' + year2 );
		        
		        var diffDate = (oDate1  -  oDate2)  /  1000  /  60  /  60  /24;//把相差的毫秒数转换为天数  
		        var iDays  =  parseInt(diffDate);
		        if(iDays >= 0){
		        	return true;
		        } else if(iDays < 0){
		        	return false
		        }
		    } else {
		    	return false;
		    }
	    } 
    </script>
</body>
</html>
